$(function () {
  //实时通知管理 打开窗口
  $('.informApk-trigger').on('click', function (event) {
    event.preventDefault();
    $('.informApk-pop').addClass('is-visible3');
    $('#informApkModal input').val('');
    informApkPageList(1);
  });
  //实时通知管理 关闭窗口
  $('.informApk-pop').on('click', function (event) {
    if (
      $(event.target).is('.cd-popup-close') ||
      $(event.target).is('.informApk-pop')
    ) {
      event.preventDefault();
      $(this).removeClass('is-visible3');
    }
  });


  // 全选 反选
  $("#informApkTableSelectAll").on("click", function () {
    var allcheck = $("#informApkTableSelectAll").prop("checked");
    $("#informApkModal tbody .check").prop("checked", allcheck);
  });
  // 反选
  $("#informApkModal tbody").on("click", '.check', function () {
    var length1 = $("#informApkModal tbody").find(":checkbox").length;
    var length2 = $("#informApkModal tbody").find(":checked").length;
    if (length1 == length2) {
      $("#informApkTableSelectAll").prop("checked", true);
    } else {
      $("#informApkTableSelectAll").prop("checked", false);
    }
  });
  //  删除模态框
  $("#informApkDelBtn").on('click', function () {
    if($('#informApkModal tbody input[type=checkbox]:checked').length>0){
      $('#informApkDelBox').addClass('is-visible3');
    }
  });  
  //  删除实时通知
  $("#informApkDelBox .delConfirm").on('click', function () {
    $('#informApkModal tbody input[type=checkbox]:checked').each(function () {
      var thisid = $(this).attr('thisid');
      var obj = {};
      obj.id = thisid
      jsonData = JSON.stringify(obj);
      $.ajax({
        url: '/info/delInform?id=' + thisid,
        type: 'delete',
        data: jsonData,
        contentType: 'application/json',
        async: false,
        success: function (res) {
          console.log(res)

        }
      });
    });
    if($("#informApkTableSelectAll").prop("checked")){
      informApkPage = informApkPage-1;
      if(informApkPage<1){
        informApkPage=1;
      }
    }
    informApkPageList(informApkPage);
    // 微型消防站管理 获取总页数 点击分页渲染列表
    informApkPageFunc(informApkPage);
    $('#informApkTableSelectAll').prop('checked', false);
    $('#informApkDelBox').removeClass('is-visible3');
    tipModalFunc('删除成功!');
  });

  var informApkPage = 1;
  var informApkPageNum = 1;

  function informApkPageFunc(pageSum, startPageNum) {
    console.log(pageSum);
    if (pageSum <= 1) {
      $('#informApkPageBox .pageJump').hide();
    } else {
      $('#informApkPageBox .pageJump').show();
    }
    Page({
      num: pageSum, //页码数
      startnum: startPageNum, //指定页码
      elem: $('#informApkPage'), //指定的元素
      callback: function (n) { //回调函数
        console.log(n)
        informApkPage = n;
        informApkPageList(n);
      }
    });
  }
  var editInformApkArr = [];
  // 渲染 实时通知 列表
  function informApkPageList(n) {
    //hasExpired 是否过期 (true过期 false未过期)
    $('#informApkModal .tableBox tbody').html('');
    var page = n - 1;
    var startTime = $('#informApkModal .startTime').val()?$('#informApkModal .startTime').val()+' 00:00:00':'2018-01-01 00:00:00';
    var endTime = $('#informApkModal .endTime').val()?$('#informApkModal .endTime').val()+' 23:59:59':'2118-01-01 00:00:00';
    var size = 10;
    
    // 获取实时通知列表
    $.ajax({
      url: '/info/getInform?beginTime='+startTime+'&endTime='+endTime+'&page=' + page + '&size='+size,
      success: function (res) {
        console.log(res);
        if (res.status == 200) {
          informApkPageNum = res.data.totalPages;
          var data = res.data.content;
          editInformApkArr = data;
          var size = res.data.size;
          for (var i = 0; i < data.length; i++) {
            var hasExpired = '';
            var content = data[i].content;
            var classStr = '';
            if (data[i].hasExpired) {
              hasExpired = '已结束';
              classStr = 'colorff4242';
            } else {
              hasExpired = '发布中';
              classStr = 'color32ffd9';
            }
            
            if (content.length > 35) {
              content = content.slice(0, 35) + ' . . .';
            }
            // 35字
            $('#informApkModal .tableBox tbody').append(`<tr>
            <td><input type="checkbox" class="check" thisid="` + data[i].id + `"/></td>
            <td>` + ((n - 1) * size + i + 1) + `</td>
            <td>` + content + `</td>
            <td>` + data[i].reportingTime + `</td>
            <td class="`+classStr+`">` + hasExpired + `</td>
            <td>
              <span class="seeBtnStyle editInformApk-trigger" index="`+i+`" thisid="`+data[i].id+`">查看详情</span>
            </td>
          </tr>`);
          }
          // 当翻到下一页时 清除掉 全选按钮的 选中状态
          $("#informApkTableSelectAll").prop("checked", false);
          informApkPageFunc(informApkPageNum, n);
        }
      }
    })
  }

  // 点击搜索，按时间查询
  $('#informApkSearchBtn').click(function(){
    informApkPageList(1);
    $("#informApkTableSelectAll").prop("checked", false);
  });

  //    实时通知添加
  // $('.addInformApk-pop').addClass('is-visible3');
  //实时通知管理添加 打开窗口
  $('.addInformApk-trigger').on('click', function (event) {
    event.preventDefault();
    $('.addInformApk-pop').addClass('is-visible3');
  });
  //实时通知管理添加 关闭窗口
  $('.addInformApk-pop').on('click', function (event) {
    if ($(event.target).is('.cd-popup-close')|| $(event.target).is('.addInformApk-pop')) {
      // 
      event.preventDefault();
      $(this).removeClass('is-visible3');
      clearAddInformApk();
    }
  });

  // 提交实时通知信息
  $('#addInformApkBtn').on('click', function () {
    var html = $('#informApkContent').val();
    var obj = {};
    var jsonData = '';
    obj.content = html;
    jsonData = JSON.stringify(obj);
    if (!html) {
      tipModalFunc('请填写完整内容!');
      return;
    }
    $.ajax({
      url: '/info/putInform?content='+html,
      // data: jsonData,
      type: 'get',
      // contentType: 'application/json',
      success: function (res) {
        console.log(res);
        $('.addInformApk-pop').removeClass('is-visible3');
        informApkPageList(informApkPage);
        // 微型消防站管理 获取总页数 点击分页渲染列表
        informApkPageFunc(informApkPage);
        clearAddInformApk();
      }
    });
  });
  // 清空实时通知添加数据
  function clearAddInformApk() {
    $('#informApkContent').val('');

  }

  var informApkIndex = '';
  //实时通知管理修改 打开窗口
  $('#informApkModal').on('click', '.editInformApk-trigger', function (event) {
    event.preventDefault();
    $('.editInformApk-pop').addClass('is-visible3');
    var index = $(this).attr('index');
    informApkIndex = index;
    var arr = editInformApkArr[index];
    var id = arr.id;
    var content = arr.content;
    $('#informApkContentEdit').val(content);
    
    // 点击查看详情时，只查看不修改
    $('#editInformApkModal textarea').attr('disabled', 'disabled');
    $('#editInformApkModal textarea').addClass('disabledStyle');
    $('#editInformApkModal .allowEdit').show();
    $('#editInformApkBtn').hide();
  });
  
  // 点击修改时,可以修改编辑
  $('#editInformApkModal .allowEdit').on('click', function () {
    $('#editInformApkModal .allowEdit').hide();
    $('#editInformApkBtn').show();
    $('#editInformApkModal textarea').removeAttr('disabled');
    $('#editInformApkModal textarea').removeClass('disabledStyle');
  });
  // 点击修改按钮
  $('#editInformApkBtn').on('click', function () {
    var html = $('#informApkContentEdit').val();
    var obj = {};
    var jsonData = '';
    var arr = editInformApkArr[informApkIndex];
    var id = arr.id;
    obj.content = html;
    obj.id = id;
    jsonData = JSON.stringify(obj);
    if (!html) {
      tipModalFunc('请填写完整内容!');
      return;
    }
    $.ajax({
      url: '/info/putInform?id='+id+'&content='+html,
      data: jsonData,
      type: 'get',
      success: function (res) {
        console.log(res);
        $('.editInformApk-pop').removeClass('is-visible3');
        informApkPageList(informApkPage);
        // 微型消防站管理 获取总页数 点击分页渲染列表
        informApkPageFunc(informApkPage);
        clearAddInformApk();
      }
    });


  });
  //实时通知管理修改 关闭窗口
  $('.editInformApk-pop').on('click', function (event) {
    if ($(event.target).is('.cd-popup-close')|| $(event.target).is('.editInformApk-pop')) {
      event.preventDefault();
      $(this).removeClass('is-visible3');
      clearEditInformApk();
    }
  });

  // 清空实时通知添加数据
  function clearEditInformApk() {
    $('#informApkContentEdit').val('');

  }
});